﻿using System.Collections.Generic;
using System.Linq;

namespace SimpleORM
{
    public class SelectObjectContext
    {
        #region Properties

        public IScriptFactory Factory { get; set; }

        public List<SelectObjectItem> SelectObjectItems { get; private set; }

        #endregion

        #region Fields

        #endregion

        #region Ctor

        public SelectObjectContext()
        {
            SelectObjectItems = new List<SelectObjectItem>();
        }

        #endregion

        #region Methods

        #region Methods - Public

        public void Install(ObjectContext oc, string fromName)
        {
            foreach (var item in oc.Columns)
            {
                SelectObjectItems.Add(new SelectObjectItem
                {
                    Factory = Factory,
                    FromName = fromName,
                    MemberName = item.Value.DataColumn.ColumnName,
                    AliasName = item.Value.PropertyInfo.Name,
                });
            }
        }

        public IScriptFactory BuildSelect(List<SelectObjectItem> items)
        {
            IScriptFactory f = items[0].GetItem();

            for (int i = 1; i < items.Count; i++)
                f = f.Concat(items[i].GetItem(), ", ");

            return f;
        }

        public IScriptFactory BuildSelect(List<IScriptFactory> items)
        {
            IScriptFactory f = items[0];

            for (int i = 1; i < items.Count; i++)
                f = f.Concat(items[i], ", ");

            return f;
        }

        public string[] GetMemberNames()
        {
            return SelectObjectItems.Select(item => item.AliasName).ToArray();
        }

        #endregion

        #endregion
    }
}
